/**
 *====================================================
 * 文件名称: WarningSchemeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2016年6月2日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.FinanceProductWarningEntity;
import com.yuexunit.finance.project.entity.WarningSchemeEntity;
import com.yuexunit.finance.project.entity.lexicon.WarningFactorEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: WarningSchemeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2016年6月2日 下午5:12:50
 */
@Implements(WarningSchemeService.class)
public class WarningSchemeServiceImpl implements WarningSchemeService {

	@Override
	public Long createWarningScheme(WarningSchemeEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getSchemeId();
	}

	@Override
	public void updateWarningScheme(WarningSchemeEntity entity) {
		WarningSchemeEntity persist = inquireWarningScheme(entity.getSchemeId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(entity);
	}

	@Override
	public void deleteWarningScheme(Long schemeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductWarningEntity.class);
		criteria.add(Restrictions.eq("schemeId", schemeId));
		List<FinanceProductWarningEntity> list = PersistUtil.get(criteria);
		for (FinanceProductWarningEntity p : list) {
			PersistUtil.delete(p);
		}
		PersistUtil.delete(WarningSchemeEntity.class, schemeId);
	}
	
	@Override
	public void deleteProductWarningScheme(Long productId, Long schemeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductWarningEntity.class);
		criteria.add(Restrictions.eq("schemeId", schemeId));
		criteria.add(Restrictions.eq("productId", productId));
		List<FinanceProductWarningEntity> list = PersistUtil.get(criteria);
		for (FinanceProductWarningEntity p : list) {
			PersistUtil.delete(p);
		}
	}

	@Override
	public WarningSchemeEntity inquireWarningScheme(Long schemeId) {
		return PersistUtil.get(WarningSchemeEntity.class, schemeId);
	}

	@Override
	public PageList<WarningSchemeEntity> inquireWarningSchemePageList(WarningFactorEnum factor, Date startDate, Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(WarningSchemeEntity.class);
		if (factor != null) {
			criteria.add(Restrictions.eq("factor", factor));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("createDateTime", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("createDateTime", endDate));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<WarningSchemeEntity> inquireProductWarningScheme(Long productId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductWarningEntity.class);
		criteria.add(Restrictions.eq("productId", productId));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("schemeId")));
		List<Long> schemeIds = PersistUtil.get(criteria);
		if (schemeIds.isEmpty()) {
			return new ArrayList<WarningSchemeEntity>(0);
		}
		criteria = DetachedCriteria.forClass(WarningSchemeEntity.class);
		criteria.add(Restrictions.in("schemeId", schemeIds));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<WarningSchemeEntity> inquireWarningSchemeList(WarningFactorEnum factor) {
		DetachedCriteria criteria = DetachedCriteria.forClass(WarningSchemeEntity.class);
		if (factor != null) {
			criteria.add(Restrictions.eq("factor", factor));
		}
		return PersistUtil.get(criteria);
	}
}
